package SORT;



public class SORT {
    /**
     * 归并排序
     * @param array
     */
    public static void mergerSort(int[]array){
        mergerSortfunc(array,0,array.length-1);
    }

    private static void mergerSortfunc(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int left=start;
        int mid=(start+end)/2;
        int right=end;
        mergerSortfunc(array,left,mid);
        mergerSortfunc(array,mid+1,right);
        merger(array,left,mid,right);
    }

    private static void merger(int[] array, int left, int mid, int right) {
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        int []tmparr=new int[right-left+1];
        int k=0;
        while(s1<=e1&&s2<=e2){
            if(array[s1]<=array[s2]){
                tmparr[k++]=array[s1++];
            }
            else{
                tmparr[k++]=array[s2++];
            }
        }
        while(s1<=e1){
            tmparr[k++]=array[s1++];
        }
        while(s2<=e2){
            tmparr[k++]=array[s2++];
        }
        for(int i=0;i<k;i++){
            array[left+i]=tmparr[i];
        }
    }
    public static void mergerSort2(int []array){
        int gap=1;
        while(gap<=array.length-1){
            for(int i=0;i<array.length;i=i+gap*2){
                int left=i;
                int mid=left+gap-1;
                int right=mid+gap;
                if(left>=array.length-1){
                    left=array.length-1;
                }
                if(right>=array.length-1){
                    right=array.length-1;
                }
                merger(array,left,mid,right);
            }
            gap*=2;
        }
    }
    /**
     * 快速排序
     */
    public static void quick(int[]array){
     quickSort(array,0,array.length-1);
    }

    private static void quickSort(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int parvoit=ParatitionHore(array,start,end);
        quickSort(array,start,parvoit-1);
        quickSort(array,parvoit+1,end);
    }
    private static int ParatitionHore(int []array,int left,int right){
        int i=left;
        int tmp=array[left];
        while(left<right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array,i,left);
        return  left;
    }



    private static void swap(int[] array, int left, int right) {
        int tmp=array[left];
        array[left]=array[right];
        array[right]=tmp;
    }
    /**
     * 归并排序训练
     */
    public static void SORTMERGE(int []array){
        SORTMERGER1(array,0,array.length-1);


    }

    private static void SORTMERGER1(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int left=start;
        int mid=(start+end)/2;
        int right=end;
        SORTMERGER1(array,left,mid);
        SORTMERGER1(array,mid+1,right);
        MERGE(array,left,mid,right);
    }

    private static void MERGE(int[] array, int left, int mid, int right) {
            int s1=left;
            int e1=mid;
            int s2=mid+1;
            int e2=right;
            int k=0;
            int []tmparr=new int[right-left+1];
            while(s1<=e1&&s2<=e2){
                if(array[s1]<=array[s2]){
                    tmparr[k++]=array[s1++];
                }
                else{
                    tmparr[k++]=array[s2++];
                }
            }
            while(s1<=e1){
                tmparr[k++]=array[s1++];
            }
            while(s2<=e2){
                tmparr[k++]=array[s2++];
            }
            for(int i=0;i<k;i++){
                array[left+i]=tmparr[i];
            }
    }
    /**
     * 非递归归并训练
     */
    public static void SORTMERGE2(int []array){
        int gap=1;
        while(gap<=array.length-1){
            for(int i=0;i<array.length;i+=gap*2){
                int left=i;
                int mid=left+gap-1;
                int right=mid+gap;
                if(left>=array.length-1){
                    left=array.length-1;
                }
                if(right>=array.length-1){
                    right=array.length-1;
                }
                MERGE(array,left,mid,right);
            }
            gap*=2;
        }
    }
    /**
     * 快排训练
     */
    public static void QUICKEXERCISE(int []array){
        QUICKSORT1(array,0,array.length-1);
    }

    private static void QUICKSORT1(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int parvoit=ParatitionHORE(array,start,end);
        QUICKSORT1(array,start,parvoit-1);
        QUICKSORT1(array,parvoit+1,end);

    }

    private static int ParatitionHORE(int[] array, int left, int right) {

        int tmp=array[left];
        int i=left;
        while(left<right){
            while(left<right&&array[right]>=tmp){
                right--;
            }
            while(left<right&&array[left]<=tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return  left;
        /**
         * z做出修改
         */

    }


}